package com.hxh.generater.springBoot.simple.gen;

import com.hxh.entity.Project;
import com.hxh.generater.springBoot.genBase.InitBaseProject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.springBoot.simple.gen
 * @ClassName: SimpleInitProject
 * @Author: HXH
 * @Description: 初始化项目方法  通过反射,动态调用
 * @Date: 2024/5/21 10:02
 */
@Slf4j
public class InitSimpleBaseProject extends InitBaseProject {

    /**
     * @return void
     * @author HXH
     * @description: 初始化数据库相关
     * @date 2024/5/21
     **/
    @Override
    public void initDatabaseData(Project project) throws Exception {
        new InitSimpleSql().createTablesAndInitData(project);
    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 初始化菜单相关
     * @date 2024/5/21
     **/
    @Override
    public void initMenu(Project project) throws Exception {
        new GenSimpleMenu().generationMenu(project);
    }


    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 初始化缓存相关
     * @date 2024/5/21
     **/
    @Override
    public void initCache(Project project) throws Exception {
        new GenSimpleCache().generationLocalCache(project);
    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 登录/权限相关
     * @date 2024/5/21
     **/
    @Override
    public void initLoginAndPermissions(Project project) throws Exception {
        GenSimpleLoginAndPermission genSimpleLoginAndPermission = new GenSimpleLoginAndPermission();
        genSimpleLoginAndPermission.generationKaptchaConfiguration(project);
        //每个模板，对应的user表应该在这里就确定好表名，username，password字段名称，不需要上层传参了
        genSimpleLoginAndPermission.generationLoginAbout(project, "user", null, null);
        genSimpleLoginAndPermission.generationGetCurrentUserUtil(project, "user");
    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 系统日志相关
     * @date 2024/5/21
     **/
    @Override
    public void initSystemLog(Project project) throws Exception {
        new GenSimpleSystemLog().generationSystemLogAbout(project, "user", "sys_log", "username");

    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 初始化工具类
     * @date 2024/5/21
     **/
    @Override
    public void initSystemUtils(Project project) throws Exception {
        new GenSimpleUtil().generationUtils(project);
    }


    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 初始化系统配置类 如跨域等
     * @date 2024/5/21
     **/
    @Override
    public void initSystemConfig(Project project) throws Exception {
        new GenSimpleSystemConfig().generationConfigAbout(project);

    }

    /**
     * @param project :
     * @return void
     * @author HXH
     * @description: 初始化系统基础代码，如pom.xml，application、yml等
     * @date 2024/5/21
     **/
    @Override
    public void initSystemBase(Project project) throws Exception {
        new GenSimpleSystemBase().generationSystemBase(project);
    }

    /**
     * @param project         :
     * @param initExtendTable
     * @return void
     * @author HXH
     * @description:
     * @date 2024/5/21 创建数据库中所有表的crud
     **/
    @Override
    public void initAllTableCRUD(Project project, boolean initExtendTable) throws Exception {
        try {
            new GenSimpleTableCRUD().generationAllTableCRUD(project, initExtendTable);
        } catch (Exception e) {
            log.error("创建数据库中所有表的crud异常", e);
            throw e;
        }

    }

    @Override
    public void initCustom(Project project) throws Exception {

    }

    /**
     * @return void
     * @author HXH
     * @description: 可选组件
     * @date 2024/5/21
     **/
    @Override
    public void initAddition(Project project) throws Exception {
        String component = project.getAdditionComponent();
        if (StringUtils.isBlank(component)) {
            return;
        }
        List<String> componentList = new ArrayList<String>(Arrays.asList(component.split(",")));
        if (componentList.contains("redis")) {
            //redis组件
            GenSimpleAddition.generationRedisCacheClass(project);
            GenSimpleAddition.processRedisMaven(project);
        }

    }
}
